1 package jrre.classloader.classfile;
2
3 import jrre.instructionset.*;
4 import jrre.instructionset.objects.*;
5 import jrre.instructionset.exceptions.*;
6 import jrre.instructionset.controlflow.*;
7 import jrre.instructionset.comparison.*;
8 import jrre.instructionset.math.*;
9 import jrre.instructionset.push.*;
10 import jrre.instructionset.push.localvariables.*;
11 import jrre.instructionset.pop.*;
12 import jrre.instructionset.pop.localvariable.*;
13 import jrre.instructionset.methodinvocation.*;
14 import jrre.instructionset.typecasting.*;
15
16 /***
17 *
18 *
19 * @author Christopher Ellsworth (Chris@chrisellsworth.com)
20 */
21 public class InstructionDecoder {
22
23 private int instructionIndex = 0;
24 private int [] instructions;
25
26 /*
27 private static int getNext(){
28 if(instructionIndex > instructions.length)
29 return
30 return instructions[instructionIndex++];
31 }
32 */
33
34 public static Instruction decode(int [] byteList){
35
36 Instruction firstInstruction = null;
37 Instruction currentInstruction = new NoOperation();
38
39 for(int i=0;i < byteList.length;i++){
40
41 switch(byteList[i]){
42
43 // aconst_null
44 case 0x01:
45 currentInstruction.setNextInstruction(new AConst_null());
46 break;
47
48 // iconst_m1
49 case 0x02:
50 currentInstruction.setNextInstruction(new IConst_m1());
51 break;
52
53 // iconst_0
54 case 0x03:
55 currentInstruction.setNextInstruction(new IConst_0());
56 break;
57
58 // iconst_1
59 case 0x04:
60 currentInstruction.setNextInstruction(new IConst_1());
61 break;
62
63 // iconst_2
64 case 0x05:
65 currentInstruction.setNextInstruction(new IConst_2());
66 break;
67
68 // iconst_3
69 case 0x06:
70 currentInstruction.setNextInstruction(new IConst_3());
71 break;
72
73 // iconst_4
74 case 0x07:
75 currentInstruction.setNextInstruction(new IConst_4());
76 break;
77
78 // iconst_5
79 case 0x08:
80 currentInstruction.setNextInstruction(new IConst_5());
81 break;
82
83 // lconst_0
84 case 0x09:
85 currentInstruction.setNextInstruction(new LConst_0());
86 break;
87
88 // fconst_0
89 case 0x0b:
90 currentInstruction.setNextInstruction(new FConst_0());
91 break;
92
93 // bipush operandOne
94 case 0x10:
95 currentInstruction.setNextInstruction(new BIPush(byteList[++i]));
96 break;
97
98 // ldc operandOne
99 case 0x12:
100 currentInstruction.setNextInstruction(new Ldc(byteList[++i]));
101 break;
102
103 // iload operandOne
104 case 0x15:
105 currentInstruction.setNextInstruction(new ILoad(byteList[++i]));
106 break;
107
108 // iload_0
109 case 0x1a:
110 currentInstruction.setNextInstruction(new ILoad_0());
111 break;
112
113 // iload_1
114 case 0x1b:
115 currentInstruction.setNextInstruction(new ILoad_1());
116 break;
117
118 // iload_2
119 case 0x1c:
120 currentInstruction.setNextInstruction(new ILoad_2());
121 break;
122
123 // iload_3
124 case 0x1d:
125 currentInstruction.setNextInstruction(new ILoad_3());
126 break;
127
128 // aload operandOne
129 case 0x19:
130 currentInstruction.setNextInstruction(new ALoad(byteList[++i]));
131 break;
132
133 // fload_0
134 case 0x22:
135 currentInstruction.setNextInstruction(new FLoad_0());
136 break;
137
138 // fload_2
139 case 0x24:
140 currentInstruction.setNextInstruction(new FLoad_2());
141 break;
142
143 // aload_0
144 case 0x2a:
145 currentInstruction.setNextInstruction(new ALoad_0());
146 break;
147
148 // aload_1
149 case 0x2b:
150 currentInstruction.setNextInstruction(new ALoad_1());
151 break;
152
153 // aload_2
154 case 0x2c:
155 currentInstruction.setNextInstruction(new ALoad_2());
156 break;
157
158 // aload_3
159 case 0x2d:
160 currentInstruction.setNextInstruction(new ALoad_3());
161 break;
162
163 // iaload
164 case 0x2e:
165 currentInstruction.setNextInstruction(new IALoad());
166 break;
167
168 // iaload
169 case 0x34:
170 currentInstruction.setNextInstruction(new CALoad());
171 break;
172
173 // istore operandOne
174 case 0x36:
175 currentInstruction.setNextInstruction(new IStore(byteList[++i]));
176 break;
177
178 // astore operandOne
179 case 0x3a:
180 currentInstruction.setNextInstruction(new AStore(byteList[++i]));
181 break;
182
183 // istore_0
184 case 0x3b:
185 currentInstruction.setNextInstruction(new IStore_0());
186 break;
187
188 // istore_1
189 case 0x3c:
190 currentInstruction.setNextInstruction(new IStore_1());
191 break;
192
193 // istore_2
194 case 0x3d:
195 currentInstruction.setNextInstruction(new IStore_2());
196 break;
197
198 // istore_3
199 case 0x3e:
200 currentInstruction.setNextInstruction(new IStore_3());
201 break;
202
203 // fstore_2
204 case 0x45:
205 currentInstruction.setNextInstruction(new FStore_2());
206 break;
207
208 // astore_0
209 case 0x4b:
210 currentInstruction.setNextInstruction(new AStore_0());
211 break;
212
213 // astore_1
214 case 0x4c:
215 currentInstruction.setNextInstruction(new AStore_1());
216 break;
217
218 // astore_2
219 case 0x4d:
220 currentInstruction.setNextInstruction(new AStore_2());
221 break;
222
223 // astore_3
224 case 0x4e:
225 currentInstruction.setNextInstruction(new AStore_3());
226 break;
227
228 // iastore
229 case 0x4f:
230 currentInstruction.setNextInstruction(new IAStore());
231 break;
232
233 // castore
234 case 0x55:
235 currentInstruction.setNextInstruction(new CAStore());
236 break;
237
238 // pop
239 case 0x57:
240 currentInstruction.setNextInstruction(new Pop());
241 break;
242
243 // dup
244 case 0x59:
245 currentInstruction.setNextInstruction(new Dup());
246 break;
247
248 // dup
249 case 0x5a:
250 currentInstruction.setNextInstruction(new Dup_x1());
251 break;
252
253 // iadd
254 case 0x60:
255 currentInstruction.setNextInstruction(new IAdd());
256 break;
257
258 // isub
259 case 0x64:
260 currentInstruction.setNextInstruction(new ISub());
261 break;
262
263 // imul
264 case 0x68:
265 currentInstruction.setNextInstruction(new IMul());
266 break;
267
268 // fmul
269 case 0x6a:
270 currentInstruction.setNextInstruction(new FMul());
271 break;
272
273 // dmul
274 case 0x6b:
275 currentInstruction.setNextInstruction(new DMul());
276 break;
277
278 // idiv
279 case 0x6c:
280 currentInstruction.setNextInstruction(new IDiv());
281 break;
282
283 // ineg
284 case 0x74:
285 currentInstruction.setNextInstruction(new INeg());
286 break;
287
288 // ishl
289 case 0x78:
290 currentInstruction.setNextInstruction(new IShl());
291 break;
292
293 // iinc
294 case 0x84:
295 currentInstruction.setNextInstruction(new IInc(byteList[++i], byteList[++i]));
296 break;
297
298 // i2f
299 case 0x86:
300 currentInstruction.setNextInstruction(new I2F());
301 break;
302
303 // f2i
304 case 0x8b:
305 currentInstruction.setNextInstruction(new F2I());
306 break;
307
308 // lcmp
309 case 0x94:
310 currentInstruction.setNextInstruction(new LCmp());
311 break;
312
313 // fcmpl
314 case 0x95:
315 currentInstruction.setNextInstruction(new FCmpl());
316 break;
317
318 // fcmpg
319 case 0x96:
320 currentInstruction.setNextInstruction(new FCmpg());
321 break;
322
323 // ifeq operandOne operandTwo
324 case 0x99:
325 currentInstruction.setNextInstruction(new Ifeq(byteList[++i], byteList[++i]));
326 break;
327
328 // ifne operandOne operandTwo
329 case 0x9a:
330 currentInstruction.setNextInstruction(new Ifne(byteList[++i], byteList[++i]));
331 break;
332
333 // iflt operandOne operandTwo
334 case 0x9b:
335 currentInstruction.setNextInstruction(new Iflt(byteList[++i], byteList[++i]));
336 break;
337
338 // ifge operandOne operandTwo
339 case 0x9c:
340 currentInstruction.setNextInstruction(new Ifge(byteList[++i], byteList[++i]));
341 break;
342
343 // ifgt operandOne operandTwo
344 case 0x9d:
345 currentInstruction.setNextInstruction(new Ifgt(byteList[++i], byteList[++i]));
346 break;
347
348 // ifle operandOne operandTwo
349 case 0x9e:
350 currentInstruction.setNextInstruction(new Ifle(byteList[++i], byteList[++i]));
351 break;
352
353 // if_icmpeq operandOne operandTwo
354 case 0x9f:
355 currentInstruction.setNextInstruction(new If_icmpeq(byteList[++i], byteList[++i]));
356 break;
357
358 // if_icmpne operandOne operandTwo
359 case 0xa0:
360 currentInstruction.setNextInstruction(new If_Icmpne(byteList[++i], byteList[++i]));
361 break;
362
363 // if_icmplt operandOne operandTwo
364 case 0xa1:
365 currentInstruction.setNextInstruction(new If_Icmplt(byteList[++i], byteList[++i]));
366 break;
367
368 // if_icmpge operandOne operandTwo
369 case 0xa2:
370 currentInstruction.setNextInstruction(new If_Icmpge(byteList[++i], byteList[++i]));
371 break;
372
373 // if_icmpgt operandOne operandTwo
374 case 0xa3:
375 currentInstruction.setNextInstruction(new If_Icmpgt(byteList[++i], byteList[++i]));
376 break;
377
378 // if_icmple operandOne operandTwo
379 case 0xa4:
380 currentInstruction.setNextInstruction(new If_Icmple(byteList[++i], byteList[++i]));
381 break;
382
383 // if_acmpeq operandOne operandTwo
384 case 0xa5:
385 currentInstruction.setNextInstruction(new If_acmpeq(byteList[++i], byteList[++i]));
386 break;
387
388 // if_acmpne operandOne operandTwo
389 case 0xa6:
390 currentInstruction.setNextInstruction(new If_acmpne(byteList[++i], byteList[++i]));
391 break;
392
393 // goto operandOne operandTwo
394 case 0xa7:
395 currentInstruction.setNextInstruction(new Goto(byteList[++i], byteList[++i]));
396 break;
397
398 // ireturn
399 case 0xac:
400 currentInstruction.setNextInstruction(new IReturn());
401 break;
402
403 // areturn
404 case 0xb0:
405 currentInstruction.setNextInstruction(new AReturn());
406 break;
407
408 // return
409 case 0xb1:
410 currentInstruction.setNextInstruction(new Return());
411 break;
412
413 // getstatic operandOne operandTwo
414 case 0xb2:
415 currentInstruction.setNextInstruction(new GetStatic(byteList[++i], byteList[++i]));
416 break;
417
418 // putstatic operandOne operandTwo
419 case 0xb3:
420 currentInstruction.setNextInstruction(new PutStatic(byteList[++i], byteList[++i]));
421 break;
422
423 // getfield operandOne operandTwo
424 case 0xb4:
425 currentInstruction.setNextInstruction(new GetField(byteList[++i], byteList[++i]));
426 break;
427
428 // putfield operandOne operandTwo
429 case 0xb5:
430 currentInstruction.setNextInstruction(new PutField(byteList[++i], byteList[++i]));
431 break;
432
433 // new operandOne operandTwo
434 case 0xbb:
435 currentInstruction.setNextInstruction(new New(byteList[++i], byteList[++i]));
436 break;
437
438 // newarray operandOne
439 case 0xbc:
440 currentInstruction.setNextInstruction(new NewArray(byteList[++i]));
441 break;
442
443 // anewarray operandOne
444 case 0xbd:
445 currentInstruction.setNextInstruction(new ANewArray(byteList[++i],byteList[++i]));
446 break;
447
448 // invokevirtual operandOne operandTwo
449 case 0xb6:
450 currentInstruction.setNextInstruction(new InvokeVirtual(byteList[++i], byteList[++i]));
451 break;
452
453 // invokespecial operandOne operandTwo
454 case 0xb7:
455 currentInstruction.setNextInstruction(new InvokeSpecial(byteList[++i], byteList[++i]));
456 break;
457
458 // invokestatic operandOne operandTwo
459 case 0xb8:
460 currentInstruction.setNextInstruction(new InvokeStatic(byteList[++i], byteList[++i]));
461 break;
462
463 // arraylength
464 case 0xbe:
465 currentInstruction.setNextInstruction(new ArrayLength());
466 break;
467
468 // athrow
469 case 0xbf:
470 currentInstruction.setNextInstruction(new AThrow());
471 break;
472
473 // checkcast
474 case 0xc0:
475 currentInstruction.setNextInstruction(new CheckCast(byteList[++i],byteList[++i]));
476 break;
477
478 // instanceof
479 case 0xc1:
480 currentInstruction.setNextInstruction(new InstanceOf(byteList[++i],byteList[++i]));
481 break;
482
483 // monitorenter
484 case 0xC2:
485 currentInstruction.setNextInstruction(new MonitorEnter());
486 break;
487
488 // monitorexit
489 case 0xC3:
490 currentInstruction.setNextInstruction(new MonitorExit());
491 break;
492
493 // ifnull
494 case 0xC6:
495 currentInstruction.setNextInstruction(new Ifnull(byteList[++i], byteList[++i]));
496 break;
497
498 // ifnonnull
499 case 0xC7:
500 currentInstruction.setNextInstruction(new Ifnonnull(byteList[++i], byteList[++i]));
501 break;
502
503 default:
504 System.out.println("decoding unimplemented: 0x"+Integer.toHexString(byteList[i]));
505 currentInstruction.setNextInstruction(new NoOperation("unimplemented: 0x"+Integer.toHexString(byteList[i])));
506 break;
507
508 }
509 //System.out.println("decoding: "+currentInstruction);
510
511 Instruction prevInstruction = currentInstruction;
512 currentInstruction = currentInstruction.getNextInstruction();
513 currentInstruction.setPrevInstruction(prevInstruction);
514
515 if(firstInstruction == null)
516 firstInstruction = currentInstruction;
517
518 if(currentInstruction == null)
519 break;
520
521 }
522
523 return firstInstruction;
524
525 }
526
527 }
528
This page was automatically generated by Maven